package com.sinoglobal.ningxia.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullListView;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.IBase;
import com.sinoglobal.ningxia.activity.amusement.BusinessDetailActivity;
import com.sinoglobal.ningxia.activity.amusement.FilterRequirement;
import com.sinoglobal.ningxia.adapter.AmusementInfoAdapter;
import com.sinoglobal.ningxia.beans.AmusementListVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.thread.ItktOtherAsyncTask;
import com.sinoglobal.ningxia.utils.ItktLog;

/**
 * @Title: AmusementInfoFragment.java
 * @Package com.sinoglobal.ningxia.fragment
 * @Description: TODO(娱乐信息展示列表)
 * @author czz
 * @date 2014-6-20 下午4:47:31
 */
public class AmusementInfoFragment extends Fragment implements AbOnListViewListener, IBase {

	private static Context context;
	private View inflate;
	private AbPullListView mListView; // 娱乐 信息列表
	private static final String limit = "10";
	private int pageNum = 0;
	private boolean onRefreshflag = true; // 刷新标识
	private boolean onLoadMoreflag = true; // 加载更多标识
	private AmusementInfoAdapter amusementInfoAdapter;
	private TextView noDataReminder;// 无数据的提示
	private ProgressBar progressBar1;
	private int type; // 1 为列表页 2 为搜索页
	private String searchStr; // 搜索内容

	public static AmusementInfoFragment newInstance(Context context, int type, String searchStr) {
		AmusementInfoFragment.context = context;
		AmusementInfoFragment amusementInfoFragment = new AmusementInfoFragment();
		Bundle bundle = new Bundle();
		bundle.putInt("type", type);
		bundle.putString("searchStr", searchStr);
		amusementInfoFragment.setArguments(bundle);
		return amusementInfoFragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle arguments = getArguments();
		type = arguments.getInt("type");
		searchStr = arguments.getString("searchStr");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (context == null)
			context = FlyApplication.context;
		try {
			inflate = LayoutInflater.from(context).inflate(R.layout.layout_amusementinfo_view, null);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		init();
		showListener();
		return inflate;
	}

	@Override
	public void onRefresh() {
		if (onRefreshflag) {
			pageNum = 0;
			if (type == 1) {
				loadInfo(0);
			} else {
				loadInfoSearch(0);
			}
			onRefreshflag = false;
		}
	}

	@Override
	public void onLoadMore() {
		if (onLoadMoreflag) {
			pageNum++;
			if (type == 1) {
				loadInfo(1);
			} else {
				loadInfoSearch(1);
			}
			onLoadMoreflag = false;
		}

	}

	@Override
	public void init() {
		ItktLog.e("按区域:" + FilterRequirement.quyu + "\n 按分类:" + FilterRequirement.classid + "\n 热度:" + FilterRequirement.star
				+ "\n 距离:" + FilterRequirement.juli);
		mListView = (AbPullListView) inflate.findViewById(R.id.listView);
		noDataReminder = (TextView) inflate.findViewById(R.id.textView1);
		progressBar1 = (ProgressBar) inflate.findViewById(R.id.progressBar1);
		mListView.getHeaderView().setHeaderProgressBarDrawable(this.getResources().getDrawable(R.drawable.progress_circular));
		mListView.getFooterView().setFooterProgressBarDrawable(this.getResources().getDrawable(R.drawable.progress_circular));
		amusementInfoAdapter = new AmusementInfoAdapter(context, 1);
		mListView.setAdapter(amusementInfoAdapter);// BusinessDetailActivity
		// 请求接口获取娱乐列表
		onRefresh();
	}

	private void loadInfo(final int type) {
		new ItktOtherAsyncTask<Void, Void, AmusementListVo>(false) {

			@Override
			public AmusementListVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().getAmusementListVo(FilterRequirement.TYPE, FilterRequirement.quyu,
						FilterRequirement.classid, FilterRequirement.star, FilterRequirement.juli, pageNum + "", limit);
			}

			@Override
			public void after(AmusementListVo result) {
				progressBar1.setVisibility(View.GONE);
				if (Code.CODE_SUCCESS == result.getCode()) {
					if(result.getList().size() > 0){
						mListView.setVisibility(View.VISIBLE);
						if(pageNum == 0 && result.getList().size() < 10){
							mListView.setPullLoadEnable(false);
						}else{
							mListView.setPullLoadEnable(true);
						}	
						if (type == 0) {
							amusementInfoAdapter.setList(result.getList());
							mListView.stopRefresh();
							onRefreshflag = true;
						} else {	
							amusementInfoAdapter.setMoreList(result.getList());
							mListView.stopLoadMore();
							onLoadMoreflag = true;
						}
						amusementInfoAdapter.notifyDataSetChanged();
					}else{
						if(pageNum == 0){
							onRefreshflag = true;
							noDataReminder.setVisibility(View.VISIBLE);
							mListView.setVisibility(View.GONE);
						}else{
							mListView.stopLoadMore();
							mListView.setPullLoadEnable(false);
							onLoadMoreflag = true;
							Toast.makeText(FlyApplication.context, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
						}
					}
					
				} else {
					onRefreshflag = true;
					noDataReminder.setVisibility(View.VISIBLE);
					mListView.setVisibility(View.GONE);
				}
			}

			@Override
			public void exception() {

			}
		}.execute();
	}

	// 加载娱乐数据信息
	private void loadInfoSearch(final int type) {
		new ItktOtherAsyncTask<Void, Void, AmusementListVo>(false) {

			@Override
			public AmusementListVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().getAmusementListSearch(FilterRequirement.TYPE, searchStr, pageNum + "", limit);
			}

			@Override
			public void after(AmusementListVo result) {
				progressBar1.setVisibility(View.GONE);
				if (Code.CODE_SUCCESS == result.getCode()) {
					if(result.getList().size() > 0){
						if(pageNum == 0 && result.getList().size() < 10){
							mListView.setPullLoadEnable(false);
						}
						mListView.setVisibility(View.VISIBLE);
						if (type == 0) {
							amusementInfoAdapter.setList(result.getList());
							mListView.stopRefresh();
							onRefreshflag = true;
						} else {
							amusementInfoAdapter.setMoreList(result.getList());
							mListView.stopLoadMore();
							onLoadMoreflag = true;
						}
						amusementInfoAdapter.notifyDataSetChanged();
					}else{
						if(pageNum == 0){
							onRefreshflag = true;
							noDataReminder.setVisibility(View.VISIBLE);
							mListView.setVisibility(View.GONE);
						}else{
							mListView.stopLoadMore();
							mListView.setPullLoadEnable(false);
							onLoadMoreflag = true;
							Toast.makeText(FlyApplication.context, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
						}
					}
					
				} else {
					onRefreshflag = true;
					noDataReminder.setVisibility(View.VISIBLE);
					mListView.setVisibility(View.GONE);
				}
			}

			@Override
			public void exception() {

			}
		}.execute();
	}

	@Override
	public void showListener() {
		mListView.setAbOnListViewListener(this);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent(getActivity(), BusinessDetailActivity.class);
				intent.putExtra(BusinessDetailActivity.SHOPID, amusementInfoAdapter.getItem(arg2 - 1).getId());
				intent.putExtra(BusinessDetailActivity.SHOPNAME, amusementInfoAdapter.getItem(arg2 - 1).getShop_name());
				intent.putExtra(BusinessDetailActivity.TYPEID, FilterRequirement.TYPE);
				startActivity(intent);
			}
		});
		noDataReminder.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				progressBar1.setVisibility(View.VISIBLE);
				noDataReminder.setVisibility(View.GONE);
				onRefresh();
			}
		});
	}
}
